package com.cn.zy.hr.redis.serviceImpl;

import com.cn.zy.hr.redis.dao.UserDao;
import com.cn.zy.hr.redis.entity.UserInfoEntity;
import com.cn.zy.hr.redis.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.List;
import java.util.Optional;

/**
 * @author zhangyi
 * @date 2018/11/28 18:52
 */
@Service
public class UserServiceImpl implements UserService
        ,Serializable{

    @Autowired
    private UserDao userDao;


    @Cacheable(value = "test_user",key = "1")
    @Override
    public List<UserInfoEntity> findAll() {
        return userDao.findAll();
    }


    @CachePut(value = "test_user",key = "T(String).valueOf(#p0.id)")
    @Override
    public UserInfoEntity updateUser(UserInfoEntity userInfoEntity) {
        Long id = userInfoEntity.getId();
        String userName = userInfoEntity.getUserName();
        String password = userInfoEntity.getPassword();
        userDao.updateUser(id, userName, password);
        return  userDao.findById(id).get();
    }

    @Cacheable(value = "test_user",key = "T(String).valueOf(#id)")
    @Override
    public UserInfoEntity getUser(Long id) {
        Optional<UserInfoEntity> byId = userDao.findById(id);
        UserInfoEntity userInfoEntity = byId.get();
        System.out.println(userInfoEntity);
        return userInfoEntity;
    }
}
